package com.atguigu.zy.interview.bug;

import java.util.concurrent.TimeUnit;

class HoldLockThread implements Runnable {
    private String lockA;
    private String lockB;

    public HoldLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "\t 自己持有：" + lockA + "尝试获取:" + lockB);

            //@formatter:off  线程暂停一会儿
            try { TimeUnit.SECONDS.sleep( 2 ); } catch (InterruptedException e) { e.printStackTrace();}
            //@formatter:on

            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "\t 自己持有：" + lockB + "尝试获取:" + lockA);
            }
        }
    }
}

/**
 * @author Jungle
 * @create 2022-09-03 20:38
 * <p>
 * 死锁是两个或者以上的进程在执行过程中，
 * 因抢占资源而造成的一种相互等待的现象，
 * 若无外力干涉那他们将无法推进下去
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "locKB";
        new Thread(new HoldLockThread(lockA, lockB), "ThreadAAA").start();
        new Thread(new HoldLockThread(lockB, lockA), "ThreadBBB").start();

        /**
         * 1、linux  ps -ef|grep  xxxx
         *
         */
        //CompletableFuture
    }
}
